package com.a0soft.gphone.aTruffleHog.wnd.info;
/*
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 3, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * Copyright(C) 2011 
 *   Patrick Cheng <patrick.taiwan (at) gmail.com>
 *
 */

import android.content.Context;
import android.content.res.Configuration;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout.LayoutParams;

import com.a0soft.gphone.aTruffleHog.R;
import com.a0soft.gphone.aTruffleHog.di.DeviceInfo;
import com.a0soft.gphone.aTruffleHog.di.DeviceInfoKey;
import com.a0soft.gphone.aTruffleHog.util.CONSTS;

public class InfoDPView {
    private static final String DP_KEYS[] = {
            DeviceInfoKey.K.DP_W,
            DeviceInfoKey.K.DP_H,
            DeviceInfoKey.K.DP_SCREEN_SIZE,
            DeviceInfoKey.K.DP_DENSITY,
            DeviceInfoKey.K.DP_SDENSITY,
            DeviceInfoKey.K.DP_DDPI,
            DeviceInfoKey.K.DP_XDPI,
            DeviceInfoKey.K.DP_YDPI,
            DeviceInfoKey.K.DP_PIXEL_FMT,
            DeviceInfoKey.K.DP_REFRESH_RATE,
            DeviceInfoKey.K.DP_FONT_SCALE,
            DeviceInfoKey.K.DP_LAYOUT_SIZE,
            DeviceInfoKey.K.DP_LAYOUT_ASPECT,
            DeviceInfoKey.K.DP_LAYOUT_SWIDTH,
            DeviceInfoKey.K.DP_LAYOUT_AWIDTH,
            DeviceInfoKey.K.DP_LAYOUT_AHEIGHT
    };

    private Context _cxt = null;
    private DeviceInfo _di = null;
    
    public
    InfoDPView(Context cxt, DeviceInfo di)
    {
    	_cxt = cxt;
    	_di = di;
    }
    
    public View
    getView(boolean for_preview)
    {
        LinearLayout sv = new LinearLayout(_cxt);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
        sv.setLayoutParams(params);
        sv.setOrientation(LinearLayout.VERTICAL);
        
        int childno = 0;
        for (int i=0; i<DP_KEYS.length; i++) {
            String info = _di.GetString(DP_KEYS[i]);
            if (!TextUtils.isEmpty(info) && !info.equals(CONSTS.UNAVAILABLE)) {
                View row = LayoutInflater.from(_cxt).inflate(R.layout.info_item,null);
                InfoItemViewWrapper wrapper = new InfoItemViewWrapper(row);
                row.setTag(wrapper);
                wrapper.setItemName(DeviceInfoKey.GetName(DP_KEYS[i]));
        		if (TextUtils.equals(DP_KEYS[i], DeviceInfoKey.K.DP_PIXEL_FMT)) {
    				info = getPixelFormatString(_di.GetInt(DP_KEYS[i]));
        		}
        		else if (TextUtils.equals(DP_KEYS[i], DeviceInfoKey.K.DP_W) ||
        				TextUtils.equals(DP_KEYS[i], DeviceInfoKey.K.DP_H)) {
    				info = _di.GetString(DP_KEYS[i]) + " " + _cxt.getResources().getString(R.string.di_dp_pixels);
        		}
        		else if (TextUtils.equals(DP_KEYS[i], DeviceInfoKey.K.DP_SCREEN_SIZE)) {
    				info = _di.GetString(DP_KEYS[i]) + " " + _cxt.getResources().getString(R.string.di_dp_inches);	        			
        		}
        		else if (TextUtils.equals(DP_KEYS[i], DeviceInfoKey.K.DP_LAYOUT_SIZE)) {
        		    int nSize = _di.GetInt(DP_KEYS[i]);
        		    String[] strs = _cxt.getResources().getStringArray(R.array.layout_sizes);
        		    info = strs[nSize];
        		}
                else if (TextUtils.equals(DP_KEYS[i], DeviceInfoKey.K.DP_LAYOUT_ASPECT)) {
                    int nSize = _di.GetInt(DP_KEYS[i]) / Configuration.SCREENLAYOUT_LONG_NO;
                    String[] strs = _cxt.getResources().getStringArray(R.array.layout_longs);
                    info = strs[nSize];
                }
                else if (TextUtils.equals(DP_KEYS[i], DeviceInfoKey.K.DP_DDPI)) {
                    int nDPI = _di.GetInt(DP_KEYS[i]);
                    String[] strs = _cxt.getResources().getStringArray(R.array.display_densities);
                    switch (nDPI) {
                    case DisplayMetrics.DENSITY_LOW: info = String.format("%d (%s)", nDPI, strs[0]); break;
                    case DisplayMetrics.DENSITY_MEDIUM: info = String.format("%d (%s)", nDPI, strs[1]); break;
                    case DisplayMetrics.DENSITY_HIGH: info = String.format("%d (%s)", nDPI, strs[2]); break;
                    case DisplayMetrics.DENSITY_XHIGH: info = String.format("%d (%s)", nDPI, strs[3]); break;
                    case DisplayMetrics.DENSITY_TV: info = String.format("%d (%s)", nDPI, strs[4]); break;
                    }
                }
                wrapper.setItemDetail(info);
                sv.addView(row);
                childno++;
            }               
        }
        return sv;
    }
    
    private String
    getPixelFormatString(int i_type)
    {
    	switch (i_type) {
    	case 256:
    		return _cxt.getResources().getString(R.string.di_dp_pf_jpeg);
    	case 20:
    		return _cxt.getResources().getString(R.string.di_dp_pf_ycbcr422i);
    	case 17:
    		return _cxt.getResources().getString(R.string.di_dp_pf_ycbcr420sp);
    	case 16:
    		return _cxt.getResources().getString(R.string.di_dp_pf_ycbcr422sp);
    	case 11:
    		return _cxt.getResources().getString(R.string.di_dp_pf_rgb332);
    	case 10:
    		return _cxt.getResources().getString(R.string.di_dp_pf_la88);
    	case 9:
    		return _cxt.getResources().getString(R.string.di_dp_pf_la8);
    	case 8:
    		return _cxt.getResources().getString(R.string.di_dp_pf_a8);
    	case 7:
    		return _cxt.getResources().getString(R.string.di_dp_pf_rgba4444);
    	case 6:
    		return _cxt.getResources().getString(R.string.di_dp_pf_rgba5551);
    	case 4:
    		return _cxt.getResources().getString(R.string.di_dp_pf_rgb565);
    	case 3:
    		return _cxt.getResources().getString(R.string.di_dp_pf_rgb888);
    	case 5:
    		return _cxt.getResources().getString(R.string.di_dp_pf_bgra8888);
    	case 2:
    		return _cxt.getResources().getString(R.string.di_dp_pf_rgbx8888);
    	case 1:
    		return _cxt.getResources().getString(R.string.di_dp_pf_rgba8888);
    	case -1:
    		return _cxt.getResources().getString(R.string.di_dp_pf_opaque);
    	case -2:
    		return _cxt.getResources().getString(R.string.di_dp_pf_transparent);
    	case -3:
    		return _cxt.getResources().getString(R.string.di_dp_pf_translucent);
    	default:
    		return _cxt.getResources().getString(R.string.di_dp_pf_unknown);
    	}
    }
}
